<template>
  <el-dialog title="明细" :close-on-click-modal="false" :visible.sync="visible">
    <el-form
      :model="dataForm"
      ref="dataFormRef"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-col :span="12">
        <el-form-item label="编码">
          <el-input v-model="dataForm.dictCode" :disabled="true"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="名称">
          <el-input v-model="dataForm.dictName" :disabled="true"></el-input>
        </el-form-item>
      </el-col>
    </el-form>
    <el-form
      :model="detailDictForm"
      :rules="detailDictRule"
      ref="detailDictFormRef"
      @keyup.enter.native="detailDictFormSubmit()"
      label-width="80px"
    >
      <el-col :span="12">
        <el-form-item label="Key" prop="dictKey">
          <el-input v-model="detailDictForm.dictKey"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="Value" prop="dictValue">
          <el-input v-model="detailDictForm.dictValue"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="描述" prop="memo">
          <el-input v-model="detailDictForm.memo"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="排序" prop="orderFlag">
          <el-input-number
            v-model="detailDictForm.orderFlag"
            :min="1"
            :max="99"
            label="排序"
          ></el-input-number>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label-width="0">
          <el-col :span="24" :push="10">
            <el-button type="primary" @click="dictDetailFormSubmit()">新增</el-button>
          </el-col>
        </el-form-item>
      </el-col>
    </el-form>
    <el-col :span="24">
      <my-default-bar barTitle="业务字典明细" />
    </el-col>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <!-- <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column> -->
      <el-table-column prop="dictName" header-align="center" align="center" label="名称">
      </el-table-column>
      <el-table-column prop="dictKey" header-align="center" align="center" label="Key">
      </el-table-column>
      <el-table-column prop="dictValue" header-align="center" align="center" label="Value">
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="80" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[6, 15, 20, 50]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataList: [],
      pageIndex: 1,
      pageSize: 6,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      dataForm: {
        id: 0,
        dictCode: '',
        dictName: '',
        memo: '',
        type: '',
        typeName: '',
      },
      detailDictForm: {
        dictKey: '',
        dictValue: '',
        memo: '',
        orderFlag: '',
      },
      detailDictRule: {
        dictKey: [{ required: true, message: 'key值不能为空', trigger: 'blur' }],
        dictValue: [{ required: true, message: 'Value值不能为空', trigger: 'blur' }],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs['dataFormRef'].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/bs/dict/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.dictCode = data.bsDict.dictCode;
              this.dataForm.dictName = data.bsDict.dictName;
              this.getDataList();
            }
          });
        }
      });
    },
    // 表单提交
    dictDetailFormSubmit() {
      this.$refs['detailDictFormRef'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/bs/dictdetail/save`),
            method: 'post',
            data: this.$http.adornData({
              dictCode: this.dataForm.dictCode,
              dictName: this.dataForm.dictName,
              dictKey: this.detailDictForm.dictKey,
              dictValue: this.detailDictForm.dictValue,
              memo: this.detailDictForm.memo,
              orderFlag: this.detailDictForm.orderFlag,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    inquire() {
      this.pageIndex = 1;
      this.getDataList();
    },
    reset() {
      this.pageIndex = 1;
      this.dataForm = this.objEmpty(this.dataForm);
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl('/bs/dictdetail/list'),
        method: 'get',
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          dictCode: this.dataForm.dictCode,
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 明细
    showDetail(id) {
      this.detailAddOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.detailAddOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.id;
          });
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/bs/dictdetail/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
  },
};
</script>
